package com.filex.ads.network;

import android.content.Context;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import com.filex.ads.common.Constants;
import com.filex.ads.common.Preconditions;
import com.filex.ads.common.logging.Logger;
import com.filex.ads.volley.Request;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;

public class AdRequestUtils {

    public static String truncateQueryParamsIfPost(@NonNull final String url) {
        Preconditions.checkNotNull(url);
        if (!isFilexRequest(url)) {
            return url;
        }

        final int queryPosition = url.indexOf('?');
        if (queryPosition == -1) {
            return url;
        }

        return url.substring(0, queryPosition);
    }

    public static boolean isFilexRequest(@NonNull final String url) {
        Preconditions.checkNotNull(url);

        final String httpHost = Constants.HTTP + "://" + Constants.HOST;
        final String httpsHost = Constants.HTTPS + "://" + Constants.HOST;

        return url.startsWith(httpHost) || url.startsWith(httpsHost);
    }

    public static int chooseMethod(String url) {
        if (isFilexRequest(url)) {
            return Request.Method.POST;
        } else {
            return Request.Method.GET;
        }
    }

    @NonNull
    public static Map<String, String> convertQueryToMap(@NonNull final Context context,
            @NonNull final String url) {
        Preconditions.checkNotNull(context);
        Preconditions.checkNotNull(url);

        final Map<String, String> params = new HashMap<>();
        final Uri uri = Uri.parse(url);
        for (final String queryParam : uri.getQueryParameterNames()) {
            params.put(queryParam, TextUtils.join(",", uri.getQueryParameters(queryParam)));
        }

        return params;
    }

    @Nullable
    public static String generateBodyFromParams(@Nullable final Map<String, String> params,
            @NonNull final String url) {
        Preconditions.checkNotNull(url);

        if (!AdRequestUtils.isFilexRequest(url) || params == null || params.isEmpty()) {
            return null;
        }

        final JSONObject jsonBody = new JSONObject();
        for (final String queryName : params.keySet()) {
            try {
                jsonBody.put(queryName, params.get(queryName));
            } catch (JSONException e) {
                Logger.d("Unable to add " + queryName + " to JSON body.");
            }
        }
        return jsonBody.toString();
    }

    /**
     * This is a helper class and should not be instantiated.
     */
    private AdRequestUtils() {
    }
}
